﻿using System.Collections;

namespace UILibrary
{
    internal class CommandHistory
    {
        private readonly ArrayList _commandHistory = new ArrayList();
        private int _currentPosn;
        private string _lastCommand;

        internal string LastCommand
        {
            get { return _lastCommand; }
        }

        internal void Add(string command)
        {
            if (command == _lastCommand)
                return;

            _commandHistory.Add(command);
            _lastCommand = command;
            _currentPosn = _commandHistory.Count;
        }

        internal bool DoesNextCommandExist()
        {
            return _currentPosn < _commandHistory.Count - 1;
        }

        internal bool DoesPreviousCommandExist()
        {
            return _currentPosn > 0;
        }

        internal string[] GetCommandHistory()
        {
            return (string[])_commandHistory.ToArray(typeof(string));
        }

        internal string GetNextCommand()
        {
            _lastCommand = (string)_commandHistory[++_currentPosn];
            return LastCommand;
        }

        internal string GetPreviousCommand()
        {
            _lastCommand = (string)_commandHistory[--_currentPosn];
            return _lastCommand;
        }
    }
}

